﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李刚 
    //
    // 文件名：UnBidDrugList.cs
    // 文件功能描述：非采购产品列表
    // 创建标识：李刚2009-06-15
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class UnBidDrugList : BasePage
    {
        public UnBidDrugList()
        {
            this.ModuleNo = Module.采购产品管理.ToString();
        }

        IBLL_Goods ig = Factory.BLL_Goods();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            StoreQueryCondition();
            SetBind();
        }

        /// <summary>
        /// 绑定所有符合条件的采购产品
        /// </summary>
        private void SetBind()
        {
            HZ.DrugTrade.Model.Query.Goods goods = GetQueryCondition();
            if (goods == null)
            {
                StoreQueryCondition();
                goods = GetQueryCondition();
            }

            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            gvwBidDrug.PageSize = AspNetPager1.PageSize;
            int totalCount = 0;
            IList<HZ.DrugTrade.Model.View.Goods> goodsList = ig.GetUnGoodsByWhere(goods, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwBidDrug.DataSource = goodsList;
            gvwBidDrug.DataBind();
        }

        /// <summary>
        /// 将查询条件存储到viewstate中
        /// </summary>
       private void StoreQueryCondition()
        {
            HZ.DrugTrade.Model.Query.Goods where = new HZ.DrugTrade.Model.Query.Goods();
            where.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    where.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            where.GoodsName = txtGoodsName.Text.Trim();
            where.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            where.Outlookc = txtOutLook.Text.Trim();
            where.Material = txtMaterial.Text.Trim();
            //where.SortName = txtProductType.Text.Trim();
            where.Unit = txtUnit.Text.Trim();
            where.GoodsName = txtGoodsName.Text.Trim();
            where.SortFields = " ProductName asc";

            ViewState["v_goods"] = where;
        }

        /// <summary>
        /// 讲查询条件从viewstate中取出
        /// </summary>
        private HZ.DrugTrade.Model.Query.Goods GetQueryCondition()
        {
            HZ.DrugTrade.Model.Query.Goods goods =(HZ.DrugTrade.Model.Query.Goods)ViewState["v_goods"];
            return goods;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        /// <summary>
        /// 简单查询与高级查询之间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                //txtCompany_TB.Text = "";
                //txtFactor.Text = "";
                txtPinyinCode.Text = "";
                //txtPack.Text = "";
                txtProduceCompany_SC.Text = "";
                //txtProductType.Text = "";
                txtUnit.Text = "";
                txtGoodsName.Text = "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwBidDrug_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtLimitPrice = e.Row.FindControl("txtLimitPrice") as TextBox;
                Label lblLimitPrice = e.Row.FindControl("lblLimitPrice") as Label;
                Label lblSortSource = e.Row.FindControl("lblSortSource") as Label;
                DropDownList ddlSortSource = e.Row.FindControl("ddlSortSource") as DropDownList;
                //设置产品属性选中条目
                //Model.View.Goods good = e.Row.DataItem as Model.View.Goods;
                //ddlSortSource.SelectedValue = good.Source.ToString();

                if (Convert.ToString(ViewState["EditStatus"]) == "1")
                {
                    txtLimitPrice.Visible = true;
                    //ddlSortSource.Visible = true;
                    lblLimitPrice.Visible = false;
                    //lblSortSource.Visible = false;

                }
                else
                {
                    txtLimitPrice.Visible = false;
                    //ddlSortSource.Visible = false;
                    lblLimitPrice.Visible = true;
                    //lblSortSource.Visible = true;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditSellPrice_Click(object sender, EventArgs e)
        {
            ViewState["EditStatus"] = "1";
            //设置按钮状态
            this.btnEditLimitPrice.Visible = false;
            this.btnSaveLimitPrice.Visible = true;

            SetBind();
        }

        /// <summary>
        /// 批量保存产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveSellPrice_Click(object sender, EventArgs e)
        {

            List<Goods> myList = new List<Goods>();
            for (int i = 0; i < gvwBidDrug.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwBidDrug.Rows[i].FindControl("checkbox1");
                TextBox txtLimitPrice = (TextBox)gvwBidDrug.Rows[i].FindControl("txtLimitPrice");
                DropDownList ddlSortSource = (DropDownList)gvwBidDrug.Rows[i].FindControl("ddlSortSource");
                Label lblGoodsName = (Label)gvwBidDrug.Rows[i].FindControl("lblGoodsName");
                if (chk.Checked == true)
                {
                    //国家限价不能为“0”
                    if (txtLimitPrice.Text.Trim() == "0")
                    {
                        HZ.Web.WebUtil.Alert("国家限价不能为0！", this);
                        txtLimitPrice.Focus();
                        return;
                    }

                    if (!string.IsNullOrEmpty(txtLimitPrice.Text.Trim()) && HZ.Common.MyValidate.IsMoney(txtLimitPrice.Text.Trim()))
                    {
                        Goods good = new Goods();
                        good.GoodsID = Convert.ToString(gvwBidDrug.DataKeys[i].Value);
                        good.GoodsName = lblGoodsName.Text;
                        good.LimitPrice = new HZ.Common.ConvertHelper(txtLimitPrice.Text.Trim()).ToDecimal;
                       //good.Source = new HZ.Common.ConvertHelper(ddlSortSource.SelectedValue).ToInt32;
                       
                        myList.Add(good);
                    }
                    else
                    {
                        HZ.Web.WebUtil.Alert("采购数量或采购价格不能为空！", this);
                        txtLimitPrice.Focus();
                        return;
                    }
                }
            }

            //循环保存
            foreach (Goods good in myList)
            {
                int result = ig.UpdateGoods( good); 
                if (result != 1)
                {
                    HZ.Web.WebUtil.Alert("更新产品“"+good.GoodsName+"”时遇到错误！", this);
                    return;
                }
            }

            ViewState["EditStatus"] = "0";
            //设置按钮状态
            this.btnEditLimitPrice.Visible = true;
            this.btnSaveLimitPrice.Visible = false;
            HZ.Web.WebUtil.Alert("产品信息修改成功！", this);

            SetBind();
        }
    }
}
